package com.atomscat.lottery.repository;

import com.atomscat.lottery.entity.db.LotteryRecordEntity;
import com.atomscat.lottery.entity.db.LotteryUserEntity;
import com.atomscat.lottery.entity.db.LotteryUserRecordEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface LotteryRecordRepository extends JpaRepository<LotteryRecordEntity, Long> {

    @Query(value = "SELECT r.id, o.nickname,o.headimgurl,p.prize_name, p.prize_type, p.description, r.lottery_state  FROM lottery_record r JOIN lottery_user o ON o.id = r.user_id JOIN lottery_prize p ON p.id = r.prize_id WHERE " +
            "r.activity_id = :activityId", nativeQuery = true)
    Page<LotteryUserRecordEntity> findByActivityId(@Param("activityId") Long activityId, Pageable pageable);

    @Query(value = "SELECT r.id, o.nickname,o.headimgurl,p.prize_name, p.prize_type, p.description, r.lottery_state  FROM lottery_record r JOIN lottery_user o ON o.id = r.user_id JOIN lottery_prize p ON p.id = r.prize_id WHERE " +
            "r.activity_id = :activityId AND r.prize_id = :prizeId", nativeQuery = true)
    Page<LotteryUserRecordEntity> findByActivityId(@Param("activityId") Long activityId, @Param("prizeId") Long prizeId, Pageable pageable);
}

